package com.jiahe.yehua.user.api;

import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import com.jiahe.yehua.common.result.Result;
import com.jiahe.yehua.common.utils.AuthContextHolder;
import com.jiahe.yehua.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 阿良
 * @version 1.0
 * @date 2021/9/6 22:14
 */
@Api(tags = "登录认证")
@RestController
@RequestMapping("/api/user")
public class UsersInfoApiController {
    @Autowired
    private UserInfoService userInfoService;


    @ApiOperation("用户手机号登录接口")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> objectMap = userInfoService.loginUser(loginVo);
        return Result.ok(objectMap);
    }


    @ApiOperation( "用户认证接口")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userInfoService.useAuth(AuthContextHolder.getUserId(request),userAuthVo);

        return Result.ok();
    }

    @ApiOperation("获取用户id信息接口")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
